﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB;
using YT.Commons;

namespace YT.XH.IgE
{
    public partial class frmIgETreatHistoryDetail : Form
    {
        
        private string mstrSeqNo = "";
        private string mstrSickRecID = "";
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrTableName = "IgETreatHistory";
        public frmIgETreatHistoryDetail(string strSeqNo,string  strSickRecID)
        {
            InitializeComponent();
            mstrSeqNo = strSeqNo;
            mstrSickRecID = strSickRecID;
            FillGrid();
        }

        private void FillGrid()
        {
            string strSQL = "Select * From "+ mstrTableName +" Where SickRecID='"+ mstrSickRecID +"' and seqno='"+ mstrSeqNo +"'";
            DataRow  drFill = mdbcon.GetDataFirstRow(strSQL);
            if (drFill != null)
            {
                txtBeforTreatRemark.Text = CvtFunc.ToString(drFill["BeforTreatRemark"]);
                txtCaseName.Text = CvtFunc.ToString(drFill["CaseName"]);
                txtRemark.Text = CvtFunc.ToString(drFill["Remark"]);
                txtSickName.Text = CvtFunc.ToString(mdbcon.GetValue("select sickname from mSickPerson Where SickRecID='" + mstrSickRecID + "'"));
                dtpExamDate.Value = CvtFunc.ToDateTime(CvtFunc.ToString(drFill["ExamDate"]));
                ChkNeedIgE.Checked = CvtFunc.ToBoolean(drFill["NeedIgE"]);
                chkNeedIgG4.Checked = CvtFunc.ToBoolean(drFill["NeedIgG4"]);
                chkNeedTotalIgG4.Checked = CvtFunc.ToBoolean(drFill["NeedTotalIgG4"]);

                txtIgEValue.Text = CvtFunc.ToDouble(drFill["IgEValue"]).ToString();
                txtIgG4Value.Text = CvtFunc.ToDouble(drFill["IgG4Value"]).ToString();
                txtTotalIgG4Value.Text = CvtFunc.ToDouble(drFill["TotalIgG4Value"]).ToString();
            }
            else
            {
                txtBeforTreatRemark.Text ="";
                txtCaseName.Text = "";
                txtRemark.Text = "";
                txtSickName.Text = CvtFunc.ToString(mdbcon.GetValue("select sickname from mSickPerson Where SickRecID='" + mstrSickRecID + "'"));
                dtpExamDate.Value = DateTime.Now.Date;
                ChkNeedIgE.Checked = false ;
                chkNeedIgG4.Checked = false ;
                chkNeedTotalIgG4.Checked = false ;

                txtIgEValue.Text ="0";
                txtIgG4Value.Text = "0";
                txtTotalIgG4Value.Text ="0";
            }
        }

        private void Update()
        {
            string strFields = @"SickRecID,SeqNo,ExamDate,Remark,BeforTreatRemark,
                                 NeedTotalIgG4,NeedIgE,NeedIgG4,CaseName,TotalIgG4ID,IgEID,IgG4ID";
            string strSQL = "Select * From "+ mstrTableName  +" Where SickRecID='"+ mstrSickRecID +"' and seqno='"+ mstrSeqNo +"'";
            DataRow drUpdate = mdbcon.GetDataFirstRow(strSQL);
            if (drUpdate == null)
            {
                DataRow drNew = mdbcon.GetDataTable(strSQL).NewRow();
                drNew["SickRecID"] = mstrSickRecID;
                drNew["SeqNo"] = 1 + CvtFunc.ToLong(mdbcon.GetValue("select max(seqno) from " + mstrTableName + " Where SickRecID='" + mstrSickRecID + "'"));
                drNew["ExamDate"] = BaseFunctions.FormatDate(dtpExamDate.Value.ToShortDateString(), DateFormatOption.DateFmtMDY);
                drNew["Remark"] = BaseFunctions.SpecialCharDeal(txtRemark.Text);
                drNew["BeforTreatRemark"] = BaseFunctions.SpecialCharDeal(txtBeforTreatRemark.Text);
                drNew["NeedTotalIgG4"] = chkNeedTotalIgG4.Checked;
                drNew["NeedIgE"] = ChkNeedIgE.Checked;
                drNew["NeedIgG4"] = chkNeedIgG4.Checked;
                drNew["CaseName"] = BaseFunctions.SpecialCharDeal(txtCaseName.Text);
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drNew, FormStatus.AddNew);
                if (!string.IsNullOrEmpty(txtCaseName.Text))
                {
                    strSQL = @"Insert Into mTreatHistory(SickRecID,TreatCaseName,BeginDate) 
                           Values ('{0}','{1}','{2}')";
                    strSQL = string.Format(strSQL, mstrSickRecID, txtCaseName.Text, BaseFunctions.FormatDate(dtpExamDate.Value.ToShortDateString(), DateFormatOption.DateFmtMDY));
                    mdbcon.ExecuteSql(strSQL);

                    strSQL = "UPDATE mSickPerson Set CurOperation='"+ txtCaseName.Text  +"' Where SickRecID='"+ mstrSickRecID +"'";
                    mdbcon.ExecuteSql(strSQL);
                }
            }
            else
            {
                drUpdate["ExamDate"] = BaseFunctions.FormatDate(dtpExamDate.Value.ToShortDateString(), DateFormatOption.DateFmtMDY);
                drUpdate["Remark"] = BaseFunctions.SpecialCharDeal(txtRemark.Text);
                drUpdate["BeforTreatRemark"] = BaseFunctions.SpecialCharDeal(txtBeforTreatRemark.Text);
                drUpdate["NeedTotalIgG4"] = chkNeedTotalIgG4.Checked;
                drUpdate["NeedIgE"] = ChkNeedIgE.Checked;
                drUpdate["NeedIgG4"] = chkNeedIgG4.Checked;
                drUpdate["CaseName"] = BaseFunctions.SpecialCharDeal(txtCaseName.Text);
                DataFunctions.UpdateTable(mdbcon, strFields, mstrTableName, drUpdate, FormStatus.Update);

                if (!string.IsNullOrEmpty(txtCaseName.Text))
                {
                    strSQL = @"Update mTreatHistory Set TreatCaseName='{0}' where SickRecID ='{1}' and BeginDate='{2}'";
                    strSQL = string.Format(strSQL, mstrSickRecID, txtCaseName.Text, BaseFunctions.FormatDate(dtpExamDate.Value.ToShortDateString(), DateFormatOption.DateFmtMDY));
                    mdbcon.ExecuteSql(strSQL);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Update();
            this.Close(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }
    }
}
